import { _decorator, Component, Label, Node } from 'cc';
import { BasePanel } from './BasePanel';
import { _game, _player, _wheel } from '../Main';
import { BaseGame } from '../games/BaseGame';
import { AudioName } from '../managers/ResourceManager';
import { Adsdk } from '../scenes/Adsdk';
const { ccclass, property } = _decorator;

@ccclass('SettingPanel')
export class SettingPanel extends BasePanel {

    panelName: string = '设置'

    @property(Node)
    quit: Node = null!

    @property(Node)
    musicOn: Node = null!

    @property(Node)
    musicOff: Node = null!

    @property(Node)
    soundOn: Node = null!

    @property(Node)
    soundOff: Node = null!

    @property(Node)
    shakeOn: Node = null!

    @property(Node)
    shakeOff: Node = null!

    start() {
        super.start()
        this.refreshButton()
        this.refreshAll()
        Adsdk.instance.showInterstitialAd()
    }

    quitGame() {
        this.out('pop', () => {
            let game: BaseGame = _game.game
            if (game) {
                game.quit()
            }
            _game.game = null
        })
    }

    onClick(e, t) {
        if (t != 'music' && t != 'sound' && t != 'shake') {
            return
        }
        _player.changeSetting(t, _player.getSetting(t) == 0 ? 1 : 0)
        this.refresh(t)
        if (t == 'music') {
            _player.switchBgm()
        }
        
    }

    refreshAll() {
        this.refresh('music')
        this.refresh('sound')
        this.refresh('shake')
    }

    refresh(setting: 'music' | 'sound' | 'shake') {
        switch(setting) {
            case 'music':
                this.musicOn.active = _player.getSetting(setting) != 0
                this.musicOff.active = !this.musicOn.active
                break
            case 'sound':
                this.soundOn.active = _player.getSetting(setting) != 0
                this.soundOff.active = !this.soundOn.active
                break
            case 'shake':
                this.shakeOn.active = _player.getSetting(setting) != 0
                this.shakeOff.active = !this.shakeOn.active
                break
        }
    }

    refreshButton() {
        this.quit.active = _game.game != null
    }
}


